using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using UMC.Data;
using UMC.Web;

using UMC.WorkFlow.Entities;

namespace UMC.WorkFlow.Subassembly
{
    /// <summary>
    /// 
    /// </summary>
    [Apiumc("WorkFlow", "IdentitySetting", Auth = WebAuthType.Admin, Desc = "配置参与人员")]
    public class IdentitySettingDesign : DesignActivity
    {
        protected override object SetValue(PropertyInfo property, object value, DesignAttribute attr)
        {
            var request = this.Context.Request;
            var config = value as IdentitySetting ?? new IdentitySetting();
            var v = this.AsyncDialog("Identity", g =>
            {
                var ui2 = UISection.Create(new UITitle(attr.Caption));
                var ui = ui2.NewSection();
                var Title = "";
                if (String.IsNullOrEmpty(config.Username) == false)
                {
                    if (config.Username.StartsWith("@"))
                    {
                        var name = config.Username.Substring(1);
                        switch (name.ToLower())
                        {
                            case "all":

                                Title = "所有通过节点";
                                ui.AddCell('\uf007', "人员账号", Title, new UIClick(request, g, "Shape"));
                                break;
                            case "start":
                                Title = "发起人";
                                ui.AddCell('\uf007', "人员账号", Title, new UIClick(request, g, "Shape"));
                                break;
                            case "prev":
                                Title = "上一层节点";
                                ui.AddCell('\uf007', "人员账号", Title, new UIClick(request, g, "Shape"));
                                break;
                            case "sql":
                                Title = "脚本获取";
                                ui.AddCell('\uf007', "人员账号", Title, new UIClick(request, g, "SQL"));
                                break;
                            case "config":

                                ui.AddCell('\uf007', "人员账号", DataFactory.Instance().TaskStaff(this.taskInstance.TaskId, DesignId)?.Value, new UIClick(request, g, "User"));
                                break;
                            default:

                                var shape = this.taskInstance.Shapes.Find(c => c.Id.StartsWith(name));
                                if (shape != null)
                                {
                                    Title = String.Format("以往结点{1}({0})", shape.Id, shape.Text);

                                    ui.AddCell('\uf007', "人员账号", Title, new UIClick(request, g, "Shape"));
                                }
                                else
                                {
                                    Title = string.Format("表单({0})", name);
                                    ui.AddCell('\uf007', "人员账号", Title, new UIClick(request, g, "Form"));
                                }

                                break;
                        }
                    }
                    else
                    {
                        Title = config.Username;
                        ui.AddCell('\uf007', "人员账号", config.Username, new UIClick(request, g, "User", "Type", "Add"));
                    }
                }
                else
                {

                    ui.AddCell('\uf007', "人员账号", "请设置");
                }

                ui = ui2.NewSection();
                ui.AddCell("设置流程节点人员", new UIClick(request, g, "Shape"));

                ui.AddCell("设置表单字段人员", new UIClick(request, g, "Form"));

                ui.AddCell("设置组织架构人员", new UIClick(request, g, "User"));

                ui.AddCell("设置脚本获取人员", new UIClick(request, g, "SQL"));
                ui2.SendTo(this.Context, "WorkFlow.Design");
                this.Context.Response.Redirect(ui2);
                return this.DialogValue("none");
            });
            switch (v)
            {
                case "SQL":
                    var sett = config.SQL ?? new SqlSetting();
                    var Settings = this.AsyncDialog("SQL", g =>
                    {
                        var form = new UIFormDialog() { Title = "SQL脚本" };
                        form.AddOption("数据库节点", "DbProviderKey", sett.DbProviderKey, sett.DbProviderKey).Command(this.Context.Request.Model, "DbProvider");
                        form.AddTextarea("SQL脚本", "SqlText", sett.SqlText).Put("tip", "").NotRequired();

                        form.Submit("确认", "WorkFlow.Design");

                        return form;
                    });
                    sett.DbProviderKey = Settings["DbProviderKey"];
                    sett.SqlText = Settings["SqlText"];
                    if (String.Equals("none", sett.SqlText, StringComparison.CurrentCultureIgnoreCase))
                    {
                        sett.SqlText = String.Empty;
                    }
                    config.SQL = sett;
                    config.Username = "@SQL";
                    return config;
                case "Shape":
                    var kId = this.AsyncDialog("Shape", g =>
                    {
                        var ts = new List<UMC.WorkFlow.Shape>
                        {
                            new Shape() { Id = "ALL", Text = "所有通过节点" },
                            new Shape() { Id = "START", Text = "发起人" },
                            new Shape() { Id = "PREV", Text = "上一层节点" }
                        };
                        ts.AddRange(this.taskInstance.Shapes);

                        var gl = UIDialog.Create(new UIGridDialog.Header("Id", 0).PutField("Text", "名称").PutField("Id", "标志"), ts.ToArray());
                        gl.Title = "流程节点";
                        return gl;
                    });


                    config.Username = "@" + kId;
                    return config;
                case "Form":
                    var kIdN = this.AsyncDialog("Shape", g =>
                    {
                        var controls = new List<TaskFormControl>
                        {
                            new TaskFormControl { Code = "设置", Caption = "[来源字典]" }
                        };

                        controls.AddRange(DataFactory.Instance().TaskFormControl(this.taskInstance.TaskId, 0).OrderBy(r => r.Seq ?? 0));//.Query(new TaskFormControl { Code = String.Empty, Caption = String.Empty }, dr => controls.Add(dr));

                        var gl = UIDialog.Create(new UIGridDialog.Header("Code", 0).PutField("Caption", "组件名称").PutField("Code", "组件编码"), controls.ToArray());
                        gl.Title = "表单组件";
                        gl.CloseEvent = "WorkFlow.Design";
                        return gl;
                    });
                    if (kIdN == "设置")
                    {
                        config.Username = String.Format("@{0}", this.AsyncDialog("User", g =>
                       {
                           return new UITextDialog() { Title = "表单名称", DefaultValue = config.Username };
                       }));
                    }
                    else
                    {
                        config.Username = String.Format("@UI.{0}", kIdN);

                    }


                    break;
                case "User":
                    var Type = this.AsyncDialog("Type", "Re");
                    var Username = this.AsyncDialog("User", "Settings", "SelectUser");

                    config.Username = "@Config";

                    var tConfig = DataFactory.Instance().TaskStaff(taskInstance.TaskId, DesignId) ?? new TaskStaff();
                    if (Type == "Add")
                    {
                        if (String.IsNullOrEmpty(tConfig.Value))
                        {
                            tConfig.Value = Username;
                        }
                        else
                        {

                            tConfig.Value = String.Format("{0} {1}", config.Username, Username);
                        }

                    }
                    else
                    {
                        tConfig.Value = Username;
                    }
                    if (DesignValue is Shape)
                    {
                        tConfig.Caption = ((Shape)DesignValue).Text;
                        tConfig.Type = 0;
                    }
                    if (DesignValue is Line)
                    {
                        tConfig.Caption = ((Line)DesignValue).Text;
                        tConfig.Type = 1;
                    }
                    if (DesignValue is Responder)
                    {
                        tConfig.Caption = ((Responder)DesignValue).Text;
                        tConfig.Type = 2;
                    }
                    tConfig.TaskId = taskInstance.TaskId;
                    tConfig.DesignKey = DesignId;
                    DataFactory.Instance().Put(tConfig);
                    break;
            }
            return config; ;

        }
    }
}
